package com.brainhealth.customer.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.core.text.Convert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.ScoreMallOrder;
import com.brainhealth.customer.service.IScoreMallOrderService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 积分商城订单Controller
 * 
 * @author wolong
 * @date 2023-11-01
 */
@RestController
@RequestMapping("/customer/mallOrder")
public class ScoreMallOrderController extends BaseController
{
    @Autowired
    private IScoreMallOrderService scoreMallOrderService;

    /**
     * 查询积分商城订单列表
     */
    @PreAuthorize("@ss.hasPermi('customer:mallOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreMallOrder scoreMallOrder)
    {
        startPage();
        List<ScoreMallOrder> list = scoreMallOrderService.selectScoreMallOrderList(scoreMallOrder);
        return getDataTable(list);
    }

    /**
     * 导出积分商城订单列表
     */
    @PreAuthorize("@ss.hasPermi('customer:mallOrder:export')")
    @Log(title = "积分商城订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScoreMallOrder scoreMallOrder)
    {
        List<ScoreMallOrder> list = scoreMallOrderService.selectScoreMallOrderList(scoreMallOrder);
        ExcelUtil<ScoreMallOrder> util = new ExcelUtil<ScoreMallOrder>(ScoreMallOrder.class);
        util.exportExcel(response, list, "积分商城订单数据");
    }

    /**
     * 获取积分商城订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:mallOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scoreMallOrderService.selectScoreMallOrderById(id));
    }

    /**
     * 新增积分商城订单
     */
    @PreAuthorize("@ss.hasPermi('customer:mallOrder:add')")
    @Log(title = "积分商城订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScoreMallOrder scoreMallOrder)
    {
        return toAjax(scoreMallOrderService.insertScoreMallOrder(scoreMallOrder));
    }

    /**
     * 修改积分商城订单
     */
    @PreAuthorize("@ss.hasPermi('customer:mallOrder:edit')")
    @Log(title = "积分商城订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScoreMallOrder scoreMallOrder)
    {
        return toAjax(scoreMallOrderService.updateScoreMallOrder(scoreMallOrder));
    }

    /**
     * 删除积分商城订单
     */
    @PreAuthorize("@ss.hasPermi('customer:mallOrder:remove')")
    @Log(title = "积分商城订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scoreMallOrderService.deleteScoreMallOrderByIds(ids));
    }

    /**
     * 使用积分购买商品
     */
    @Anonymous
    @PostMapping("/useScoreBuyGoods")
    public AjaxResult useScoreBuyGoods(HttpServletRequest request, @RequestBody Map<String, Object> map)
    {
        Long goodsId = Convert.toLong(map.get("goodsId"));
        boolean result = scoreMallOrderService.useScoreBuyGoods(request, goodsId);
        if (result)
        {
            return AjaxResult.success("购买道具成功！");
        }
        return AjaxResult.error("购买道具失败！");
    }

}
